<?php 
	//timer pengecekan load page...
	include("inc/timer.php");
	$_start_timer = get_microtime();
	 
	//check protect1 jika session da tidak valid, balikin ke index.php
	//jika valid y biarin saja
	
	require_once("protect1.php");
	require_once("inc/connect.php");
	require_once("inc/settings.php");
	require_once("inc/function.php");
	require_once("inc/function-html.php");
	
	// $pilih=mysql_select_db("uwika_krs",$conn);
	//$pdo->query("USE uwika_krs");

	//ganti dengan yg otomatis
	$_nrp = $reg_uname;
	$_semester = $_global_settings['semester']->value;
	$_tahun_ajaran = $_global_settings['tahun_ajaran']->value;
	$_hari = array('SENIN', 'SELASA','RABU', 'KAMIS','JUMAT','SABTU','BEBAS');

	$sql = "SELECT nama FROM biodata WHERE nrp = :reg_uname ";
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array(':reg_uname' => $_nrp )) or die("ERROR: " . implode(":", $stmt->errorInfo())); //reg jurusan dari protect1.php
	$_data = $stmt->fetch(PDO::FETCH_OBJ);

	//protect2 
	//kalau lambat, bisa dihapus
	//cheking semester.. biar gk aneh2 mbukak.. prevent from sql injection
	//g perlu lg deh
	$sql = "SELECT distinct semester FROM penawaran WHERE jurusan = :jurusan ORDER BY semester ASC";
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array(':jurusan' => $reg_jurusan));
	$_semester_penawaran = $stmt->fetchAll(PDO::FETCH_OBJ);	
	
	
	//mendapatkan Nilai IPS dan toleransi
	$sql = "select ips, toleransi from ips where nrp = :reg_uname";
	$stmt_ips = $pdo->prepare($sql);
	$stmt_ips->execute(array(':reg_uname' => $_nrp )) or die("ERROR: " . implode(":", $stmt_ips->errorInfo())); //reg jurusan dari protect1.php
	$_data_ips = $stmt_ips->fetch(PDO::FETCH_OBJ);
	//list($sks,$ips,$toleransi) = $row;
	
	//dapatkan Nilai
	$sql = "select sum(mk.sks) as sks_krs from registrasi, mk where nrp = :reg_uname AND mk.kodemk = registrasi.kodemk ";
	$stmt_sks_krs = $pdo->prepare($sql);
	$stmt_sks_krs->execute(array(':reg_uname' => $_nrp )) or die("ERROR: " . implode(":", $stmt_sks_krs->errorInfo()));  //reg jurusan dari protect1.php
	$_data_sks_krs = $stmt_sks_krs->fetch(PDO::FETCH_OBJ);
	//list($sks_krs) = $row;

	$limit_sks = $function->ips2sks($_data_ips->ips, (substr($_tahun_ajaran,2,2) == $_nrp[3].$_nrp[4] ? true : false) );
	// Sampai disini saja!
	
	$masa_blokir = true;
	/* cek level mahasiswa */
	$sqlstr_checking_level_blokir = "SELECT username, password, level FROM security WHERE username=:uname";
	$stmt_checking_level_blokir = $pdo->prepare($sqlstr_checking_level_blokir);
	$stmt_checking_level_blokir->execute(array(':uname' => $_nrp));
	//because of php 5.2 $_POST['name'] = $name, that makes me difficult for working -_-
	if($row_checking_level_blokir = $stmt_checking_level_blokir->fetch(PDO::FETCH_OBJ)){
		if($row_checking_level_blokir->level != 1){
			$masa_blokir = true;
		}else{
			$masa_blokir = false;
		}
	}
	/* finish */
	
	//get jurusan & dosen wali
	require_once("inc/function-db.php");
	
	$_jurusan = $function_db->getJurusan(substr($_nrp,0,3),$pdo);
	$_dosen_wali = $function_db->getDosenWali($_nrp,$pdo);

?>
<!DOCTYPE html>
<html>
<head>
<title>Student Home !</title>
<link rel="stylesheet" type="text/css" href="css/reset_main.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
<style type="text/css">
	body{ behavior:url(cssHoverFix.htc);}
	#div a { normal state }
	#div a:hover { alternative state }
</style>
</head>
<body>	
	<header id="main">
		<table id="header">
			<tr>
				<td class="header_logo_small"><a class="header_logo_small" href="#"></a></td>
				<td class="header_announce">
					<marquee style="letter-spacing:1px;marquee-speed:slow;">
						Nilai <strong>UTS</strong> Semester Gasal Periode 2012-2013 sudah keluar *sebagian. 
						Jika anda menemukan Bug / Kritik / Saran, silahkan Laporkan kepada ICT Sesegera mungkin. 
						Terima Kasih.
					</marquee>
				</td>
				<td class="header_user_name"><a class="header_user_name_a" href="#"><?php echo (strlen($_data->nama) < 15 ? ucwords(strtolower($_data->nama)) : ucwords(strtolower(substr($_data->nama,0,15)))."..."); ?></a></td>
				<td class="header_user_photo">
					<a class="header_user_photo_a" href="#">
						<img class="header_user_photo_img" src="images/people/default.gif">
					</a>
				</td>
			</tr>
		</table>
	</header>
	<table class="board">
		<tr>
			<td class="sidebar">
				<div id="navigation">
					<ul class="top-level">
						<?php
							$_level_settings = getLevelSettings($reg_level, $pdo);
							$basename_url = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
							if(count($_level_settings->menu) > 0){
								foreach($_level_settings->menu as $foreach){
									if ($basename_url == $foreach->url){$clicked = 'class="clicked"';}else{$clicked = '';}
									echo '<li><a href="'.$foreach->url.'" '.$clicked.'>'.$foreach->nama.'</a></li>';
								}
							}else{ echo '<li><a href="#" class="clicked">Home</a></li>'; }
						?>
					</ul>
				</div>
			</td>
			<td class="bigbox">
				<div class="banner"></div>
				<div class="content letter-spacing">
					<?php if($masa_blokir){ ?>
						<div class="warning red">
							<label>Account Diblokir !</label>
							Mohon Maaf, Account Anda untuk sementara waktu ini kami <strong>BLOKIR</strong>
							Jika anda belum menyelesaikan biaya daftar ulang / biaya denda atas keterlambatan KRS, silahkan menghubungi Biro Administrasi Keuangan.
							Terimakasih
						</div>
					<?php } ?>
					<!-- <div class="warning red">
						<label>Peringatan !</label>
						Anda belum menvalidasi <span class="sign border red">KRS</span> anda untuk dicek kepada <code>Dosen Wali</code> anda masing-masing.
					</div>
					<div class="warning red">
						<label>Peringatan !</label>
						Masa pengambilan <span class="sign border red">KRS</span> telah berakhir. Silahkan hubungi <code>BAA</code> untuk keperluan Administrasi lainnya.
					</div>
					<div class="warning green">
						<label>Sukses !</label>
						Terima kasih, anda telah menvalidasi <span class="sign border red">KRS</span>, 
						silahkan mencetak <span class="sign border red">KRS</span> anda di halaman <span class="sign border blue"><a href="view_krs.php">KRS Terdaftar</a></span>
						dan hubungi <code>Dosen Wali</code> anda untuk lebih lanjut.
					</div> --> <br \>
					<h1>Mata Kuliah yang Ditawarkan</h1><br \>	
					<h2>Berikut dibawah ini adalah daftar mata kuliah yang ditawarkan pada semester ini</h2>
					<div class="warning blue">
						<label>Ingat !</label>
						Untuk <span class="sign red border">Mendaftarkan</span> & <span class="sign red border">Membatalkan</span>, 
						Silahkan memilih <span class="sign blue border">Mata kuliah</span> dibawah ini. 					
						Jangan lupa untuk menverifikasikan serta mendiskusikannya kepada <code>Dosen Wali</code> anda masing - masing
					</div><br \>
					<div align="center">
						<table class="data head">
							<tr>
								<td width="15%">Periode</td><td width="2%">:</td><td width="35%"><?php echo $_tahun_ajaran; ?></td><td width="15%">NRP</td><td width="2%">:</td><td width="35%"><?php echo $_nrp; ?></td>
							</tr>
							<tr>
								<td>Semester</td><td>:</td><td><?php echo $_semester; ?></td><td>Nama</td><td>:</td><td><?php echo $_data->nama; ?></td>
							</tr>
							<tr>
								<td>Program Studi</td><td>:</td><td><?php echo strtoupper($_jurusan->nama_jurusan); ?></td><td>Dosen Wali</td><td>:</td><td><?php echo $_dosen_wali->nama; ?></td>
							</tr>
							<tr>
								<td>IPS Terakhir</td><td>:</td><td><?php echo $_data_ips->ips; ?></td><td>Limit SKS</td><td>:</td><td><?php echo $limit_sks; ?> SKS <span class="sign red border">+<?php echo $_data_ips->toleransi; ?></span></td>
							</tr>
							<tr>
								<td>Sisa SKS Anda</td><td>:</td><td><?php echo ($limit_sks + $_data_ips->toleransi - $_data_sks_krs->sks_krs); ?> SKS<td>Menyortir</td><td>:</td>
								<td>
									<select name="sort_penawaran" onchange="document.location.href='?sort_penawaran='+this.value;">
									  <option>-- Pilih --</option>
									  <option value="waktu">Waktu</option>
									  <option value="semester">Semester</option>
									</select>
								</td>
							</tr>
						</table>
						<br />
						<?php
						if( $_GET[sort_penawaran] == "semester" ){
						?>
						<!-- table sesuai semester -->	
							<?php
								$sql = "SELECT recno, penawaran.kodemk, nama, sks, dosen, keterangan, pagu, sesi, hari, mulaipukul, selesaipukul 
										FROM penawaran , mk WHERE semester = :select_semester and periode = :select_periode 
										and penawaran.kodemk = mk.kodemk and hari = :hari 
										and jurusan = :reg_jurusan order by mulaipukul";
								$stmt = $pdo->prepare($sql);
								$count = 1;
								$jmlsks = 0;
								$print_hari = true;
								
								for($j = 0; $j < count($_semester_penawaran); $j++ ){
									$colour_table = 1;
									$jmlsks_per_semester = 0;
									$colour_sign_semester = $function_html->getWarnaSemester($_semester_penawaran[$j]->semester);
							?>
							<br \>
							<table class="data body">
							<thead>
								<tr class="not-bottom">
									<th width="4%"></th><th align="left"><span class="sign <?php echo $colour_sign_semester; ?> big border">Semester   <?php echo $_semester_penawaran[$j]->semester;?></span></th>
								</tr>
							</thead>
							</table>
							<table class="data body">
							<thead>
								<tr>
									<th width="4%">No</th><th width="8%">Kode</th><th width="47%">Mata Kuliah</th>
									<th>Hari</th><th>Pukul</th><th>Status</th><th width="6%">SKS</th>
								</tr>
							</thead>
							<tbody>							
								<?php
										for($i = 0; $i < count($_hari); $i++ ){
											$stmt->execute(array(':select_semester' => $_semester_penawaran[$j]->semester
																,':select_periode' => $_tahun_ajaran
																,':hari' => $_hari[$i]
																,':reg_jurusan' => $reg_jurusan )); //reg jurusan dari protect1.php
											if($stmt->rowCount() > 0){
												while($_data = $stmt->fetch(PDO::FETCH_OBJ)){ //fetch data to stdclass object
													if($masa_blokir){
														$_data->url = "#";
													}else{
														$_data->url = "view_mk.php?kodemk=".$_data->kodemk;
													}
													?>
														<tr onclick="document.location.href='<?php echo $_data->url; ?>';" <?php echo ( ($colour_table % 2) == 0 ? 'class="odd"' : ''); ?>>
															<td class="center"><a id="block" href="<?php echo $_data->url;?>"><?php echo $count; ?></a></td>
															<td class="center"><a id="block" href="<?php echo $_data->url;?>"><?php echo $_data->kodemk; ?></a></td>														
															<td align="left"><a id="block" href="<?php echo $_data->url;?>"><?php echo $_data->nama; ?> <span class="sign <?php echo $colour_sign_semester; ?> border"><?php echo $_semester_penawaran[$j]->semester; ?></span></a></td>
															<td class="center"><a id="block" href="<?php echo $_data->url;?>"><?php if($print_hari){ echo $_hari[$i]; $print_hari = false; }?></a></td>														
															<td class="center"><a id="block" href="<?php echo $_data->url;?>"><?php echo $_data->mulaipukul."-".$_data->selesaipukul; ?></a></td>
															<td class="center"><a id="block" href="<?php echo $_data->url;?>"><?php echo $_data->status; ?></a></td>
															<td class="center"><a id="block" href="<?php echo $_data->url;?>"><?php echo $_data->sks; ?></a></td>
														</tr>
													<?php
													$count++; $colour_table++;
													$jmlsks += $_data->sks;
													$jmlsks_per_semester += $_data->sks;
												}
												$print_hari = true;
											}
										}
								?>
							</tbody>
							<tfoot>
								<tr>
									<td width="4%"></td width="8%"><td></td><td></td><td></td><td></td>
									<td width="6%">TOTAL</td><td><span class="sign black big border"><?php echo $jmlsks_per_semester; ?></span></td><td></td>
								</tr>
								<?php 
									//mbuh gae opo
									if( $j+1 == count($_semester_penawaran)){
									?>
								<tr>
									<td width="4%"></td width="8%"><td></td><td></td><td></td><td colspan="2" width="6%" style="text-align:right;">TOTAL SKS</td><td><span class="sign black big border"><?php echo $jmlsks; ?></span></td><td></td>
								</tr>
									<?php
									}
								?>
							</tfoot>
						</table>			
							<?php
								}
							?>
						<?php
						}else{
						?>
						<!-- table sesuai waktu -->	
						<?php
								$sql = "SELECT recno, penawaran.kodemk, nama, sks, dosen, keterangan, pagu, sesi, hari, mulaipukul, selesaipukul, semester 
										FROM penawaran , mk WHERE hari = :hari and periode = :select_periode 
										and penawaran.kodemk = mk.kodemk and hari = :hari 
										and jurusan = :reg_jurusan order by mulaipukul, semester, penawaran.kodemk";
								$stmt = $pdo->prepare($sql);
								$count = 1;
								$jmlsks = 0;
								$count_less_for = 1;								
								for($j = 0; $j < count($_hari); $j++ ){
									$colour_table = 1;
									$jmlsks_per_semester = 0;
									$print_hari = true;
									$stmt->execute(array(':hari' => $_hari[$j]
																,':select_periode' => $_tahun_ajaran
																,':reg_jurusan' => $reg_jurusan )); //reg jurusan dari protect1.php
									if($stmt->rowCount() > 0){
							?>
							<br \>
							<table class="data body">
							<thead>
								<tr class="not-bottom">
									<th width="4%"></th><th align="left"><span class="sign black big border"><?php echo ucwords(strtolower($_hari[$j]));?></span></th>
								</tr>
							</thead>
							</table>
							<table class="data body">
							<thead>
								<tr>
									<th width="4%">No</th><th width="8%">Kode</th><th width="47%">Mata Kuliah</th>
									<th>Hari</th><th>Pukul</th><th>Status</th><th width="6%">SKS</th>
								</tr>
							</thead>
							<tbody>							
								<?php
												while($_data = $stmt->fetch(PDO::FETCH_OBJ)){ //fetch data to stdclass object
													$colour_sign_semester = $function_html->getWarnaSemester($_data->semester);
													if($masa_blokir){
														$_data->url = "#";
													}else{
														$_data->url = "view_mk.php?kodemk=".$_data->kodemk;
													}
													?>
														<tr onclick="document.location.href='<?php echo $_data->url	; ?>';" <?php echo ( ($colour_table % 2) == 0 ? 'class="odd"' : ''); ?>>
															<td class="center"><?php echo $count; ?></td>
															<td class="center"><center><?php echo $_data->kodemk;?></td>														
															<td align="left"> <?php echo $_data->nama; ?> <span class="sign <?php echo $colour_sign_semester; ?> border"><?php echo $_data->semester; ?></span></td>
															<td class="center"><?php if($print_hari){ echo $_hari[$j]; $print_hari = false; }?></td>														
															<td class="center"><?php echo $_data->mulaipukul."-".$_data->selesaipukul; ?></td>
															<td class="center"><?php echo $_data->status; ?></td>
															<td class="center"><?php echo $_data->sks; ?></td>
														</tr>
													<?php
													$count++; $colour_table++;
													$jmlsks += $_data->sks;
													$jmlsks_per_semester += $_data->sks;
												}
												$print_hari = true;
								?>
							</tbody>
						</table>			
								<?php
									}
								}
							?>
						<?php
						} //end if else waktu/semester
						?>
						...	
						<br \><br \><br \>
					</div>
				</div>
			</td>
		</tr>
	</table>
	<footer id="main">
	  <div><a href="http://www.widyakartika.ac.id/perpustakaan">Perpustakaan Online</a> | <a href="http://krs.widyakartika.ac.id/">KRS Online</a></div>
	  <div>Copyright &copy; 2012. <a href="http://www.widyakartika.ac.id">Universitas Widya Kartika</a> & <a href="#">Labkom ICT</a> - All rights reserved</div>
		<?php
			$_finish_timer = get_microtime();
			echo 'Page generated in '.round(($_finish_timer - $_start_timer), 4).' seconds.';
		?>
	</footer>
</body>
